# HarmonyOS Project Verification Script
# Check if the project can run properly

Write-Host "Checking HarmonyOS Project..." -ForegroundColor Green
Write-Host "=" * 50

# Check current directory
$currentDir = Get-Location
Write-Host "Current Directory: $currentDir" -ForegroundColor Cyan

# Check project files
Write-Host "`nChecking project file structure..." -ForegroundColor Yellow

$requiredFiles = @(
    "build-profile.json5",
    "hvigorfile.ts", 
    "oh-package.json5",
    "entry\src\main\ets\pages\Index.ets",
    "entry\src\main\ets\pages\injuryAssessment.ets",
    "entry\src\main\ets\pages\rehabilitationLibrary.ets",
    "entry\src\main\ets\pages\myRehabilitationPlans.ets"
)

$missingFiles = @()
foreach ($file in $requiredFiles) {
    if (Test-Path $file) {
        Write-Host "OK $file" -ForegroundColor Green
    } else {
        Write-Host "MISSING $file" -ForegroundColor Red
        $missingFiles += $file
    }
}

# Check Node.js and npm
Write-Host "`nChecking development tools..." -ForegroundColor Yellow

try {
    $nodeVersion = node --version 2>$null
    if ($nodeVersion) {
        Write-Host "OK Node.js: $nodeVersion" -ForegroundColor Green
    } else {
        Write-Host "MISSING Node.js not installed" -ForegroundColor Red
    }
} catch {
    Write-Host "MISSING Node.js not installed or not in PATH" -ForegroundColor Red
}

try {
    $npmVersion = npm --version 2>$null
    if ($npmVersion) {
        Write-Host "OK npm: v$npmVersion" -ForegroundColor Green
    } else {
        Write-Host "MISSING npm not installed" -ForegroundColor Red
    }
} catch {
    Write-Host "MISSING npm not installed or not in PATH" -ForegroundColor Red
}

# Check ohpm
Write-Host "`nChecking HarmonyOS tools..." -ForegroundColor Yellow

try {
    $ohpmVersion = ohpm -v 2>$null
    if ($ohpmVersion) {
        Write-Host "OK ohpm: $ohpmVersion" -ForegroundColor Green
        
        # Check ohpm configuration
        try {
            $registry = ohpm config get registry 2>$null
            Write-Host "Registry: $registry" -ForegroundColor Cyan
        } catch {
            Write-Host "WARNING Cannot get ohpm registry config" -ForegroundColor Yellow
        }
    } else {
        Write-Host "MISSING ohpm not installed" -ForegroundColor Red
    }
} catch {
    Write-Host "MISSING ohpm not installed or not in PATH" -ForegroundColor Red
}

# Check TypeScript
try {
    $tscVersion = tsc --version 2>$null
    if ($tscVersion) {
        Write-Host "OK TypeScript: $tscVersion" -ForegroundColor Green
    } else {
        Write-Host "WARNING TypeScript not globally installed" -ForegroundColor Yellow
    }
} catch {
    Write-Host "WARNING TypeScript not globally installed" -ForegroundColor Yellow
}

# Check project configuration
Write-Host "`nChecking project configuration..." -ForegroundColor Yellow

if (Test-Path "oh-package.json5") {
    Write-Host "OK oh-package.json5 exists" -ForegroundColor Green
}

if (Test-Path "build-profile.json5") {
    Write-Host "OK build-profile.json5 exists" -ForegroundColor Green
}

if (Test-Path "hvigorfile.ts") {
    Write-Host "OK hvigorfile.ts exists" -ForegroundColor Green
}

if (Test-Path "entry\oh-package.json5") {
    Write-Host "OK entry module configuration exists" -ForegroundColor Green
}

# Generate verification report
Write-Host "`nVerification Report" -ForegroundColor Green
Write-Host "=" * 50

$issues = @()
$recommendations = @()

if ($missingFiles.Count -gt 0) {
    $issues += "Missing critical project files: $($missingFiles -join ', ')"
}

try {
    $nodeCheck = node --version 2>$null
    if (-not $nodeCheck) {
        $issues += "Node.js not installed"
        $recommendations += "Install Node.js 18.x or higher"
    }
} catch {
    $issues += "Node.js not installed"
    $recommendations += "Install Node.js 18.x or higher"
}

try {
    $ohpmCheck = ohpm -v 2>$null
    if (-not $ohpmCheck) {
        $issues += "ohpm not installed"
        $recommendations += "Configure ohpm tools according to setup guide"
    }
} catch {
    $issues += "ohpm not installed"
    $recommendations += "Configure ohpm tools according to setup guide"
}

if ($issues.Count -eq 0) {
    Write-Host "SUCCESS Project verification passed!" -ForegroundColor Green
    Write-Host "OK All necessary files and tools are ready" -ForegroundColor Green
    Write-Host "Next steps:" -ForegroundColor Cyan
    Write-Host "   1. Install DevEco Studio" -ForegroundColor White
    Write-Host "   2. Import this project to DevEco Studio" -ForegroundColor White
    Write-Host "   3. Configure HarmonyOS SDK" -ForegroundColor White
    Write-Host "   4. Run the application" -ForegroundColor White
} else {
    Write-Host "WARNING Found the following issues:" -ForegroundColor Yellow
    foreach ($issue in $issues) {
        Write-Host "   - $issue" -ForegroundColor Red
    }
    
    if ($recommendations.Count -gt 0) {
        Write-Host "`nRecommended solutions:" -ForegroundColor Cyan
        foreach ($rec in $recommendations) {
            Write-Host "   - $rec" -ForegroundColor White
        }
    }
    
    Write-Host "`nPlease check harmonyos_setup_guide.md for detailed installation guide" -ForegroundColor Cyan
}

Write-Host "`nUseful links:" -ForegroundColor Cyan
Write-Host "   - Project preview: http://127.0.0.1:8080/preview.html" -ForegroundColor White
Write-Host "   - Setup guide: ./harmonyos_setup_guide.md" -ForegroundColor White
Write-Host "   - Project analysis: ./project_analysis.md" -ForegroundColor White
Write-Host "   - DevEco Studio: https://developer.harmonyos.com/cn/develop/deveco-studio" -ForegroundColor White

Write-Host "`nVerification completed!" -ForegroundColor Green